﻿// Currently stackwalk is not doing anything except setting the registries.
//Will be set in the next version
// Need to enable the switch for PoolTag. Everything else is fine.
//Also need to verify that the commands are being generated accurately.


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;


namespace Xperf123
{

    public partial class Form1 : Form
    {

        string xppara1, xpe, xppara2, xppara3, xptype, xpfile, xpmode, xppath, xparg, targ, ht1, ht2;
        // xpe is for the name of the executalbe.
        // xparg is the full arguement/parameter being passed to the xpe exe.
        // since the process start info needs the exe and the argument.
        //xptype is the portion of the parameter being prepared in the advanced mode
        //xpmode is for the file logging mode. circular or not
        //xppath for knowing the location of the file if we are saving it at the user specified location.
        //targ temp 



        string time_start, time_stop;
        DateTime dt = DateTime.Now;
        string fname, fpath, fp;
        string os_name;
        string archi;
        string lmnt5, lmnt6, lmstp, lms;
        Process myProcess = new Process();
        DialogResult msgr;
        string hpid; // for the heap leak process id
        int t; //to check if i really want to start the process or not
        int m = 1; // this for knowing which exe i am  calling xperf or xbootmgr. if its 1 then xperf, if 2 then its xbootmgr.
        string cd; //for getting the current date.
        string tn; //for the name of the pc
        int ab; // the value of the drop down box selection
        string xb; // for xbootmgr buffer
        string xst; // for xbootmgr stacktracing option
        string st; // for stack trace parameters
        string pt; //for pooltag info
        int archival;  // for 32bit and 64bit machine so tht i can use the appropriate stackwalks


        public void chkarchi() // for checking if the machine is 32bit or 64 bit
        {
            if (IntPtr.Size == 8)
            {
                archi = "64bit machine";
                archival = 64;

            }
            else if (IntPtr.Size == 4)
            {
                archi = "32 bit machine";
                archival = 32;
            }
        } // end of archi check

        public void starttheprocess(int a)
        {
            if (a == 1) //to check if i really want to start the process or not
            {
                //Turn on the hourglass cursor here.
                try
                {
                    this.Cursor = Cursors.WaitCursor;
                    myProcess.StartInfo.ErrorDialog = true;
                    myProcess.StartInfo.UseShellExecute = true;
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    myProcess.Start();
                    myProcess.WaitForExit();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

                //Turn off the hourglass cursor here.
                this.Cursor = Cursors.Default;

            }

        } //end of the start process function

        public void xbmadv() //for setting the buffer size and the stack options in the advanced menu in xbootmgr
        {
            xb = textBox11.Text;
            xst = textBox12.Text;
            // for the buffersize 
            RegistryKey xbmk = Registry.LocalMachine;
            xbmk = xbmk.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
            if (xbmk != null) // checking if the key exists
            {

                xbmk.SetValue("BufferSize", xb, RegistryValueKind.DWord);
                xbmk.Close();
            }
            else  //Registry Key does not exist. Creating it now
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession does not exist. Creating it now");
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession");
                RegistryKey xbmk1 = Registry.LocalMachine;
                xbmk1 = xbmk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
                xbmk1.SetValue("BufferSize", xb, RegistryValueKind.DWord);
                xbmk1.Close();
            }


            // For the Global logger

            RegistryKey xbmk2 = Registry.LocalMachine;
            xbmk2 = xbmk2.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger", true); //opening the key as writeable
            if (xbmk2 != null) // checking if the key exists
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger exists. Working on the subkey now.");
                xbmk2.SetValue("BufferSize", xb, RegistryValueKind.DWord);
                xbmk2.Close();
            }//close if

            else
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger does not exist. Creating it now");
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger");
                RegistryKey xbmk3 = Registry.LocalMachine;
                xbmk3 = xbmk3.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger", true); //opening the key as writeable
                xbmk3.SetValue("BufferSize", xb, RegistryValueKind.DWord);
                xbmk3.Close();

            }//close else 
            //end global loggger


            //start stack tracing for xbootmgr
            RegistryKey xbmk4 = Registry.LocalMachine;
            xbmk4 = xbmk4.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
            if (xbmk4 != null) // checking if the key exists
            {
                MessageBox.Show("We have a value already set for the EnableProperty but its not 4 that we need to set. Setting it up now", "Value already present", MessageBoxButtons.OK, MessageBoxIcon.Information);
                xbmk4.SetValue("EnableProperty", xst, RegistryValueKind.DWord);
                xbmk4.Close();
            }
            else  //Registry Key does not exist. Creating it now
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession does not exist. Creating it now");
                RegistryKey xbmk5 = Registry.LocalMachine;
                xbmk5.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession");
                xbmk5 = xbmk5.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
                xbmk5.SetValue("EnableProperty", xst, RegistryValueKind.DWord);
                xbmk5.Close();
            }
            //end stack tracing for xbootmgr

        } // ending function


        //The function to make the general registry changes for the xbootmgr for the buffersize of xbootmgr in registry
        public void xbmgen()
        {

            //For the WMI\\Autologger\\XbootMgrSession
            RegistryKey xbmk = Registry.LocalMachine;
            xbmk = xbmk.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
            if (xbmk != null) // checking if the key exists
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession exists. Working on the subkey now.");
                //checking if the Buffersize already exist                
                if (xbmk.GetValue("BufferSize") != null)
                {
                    string ret = xbmk.GetValue("BufferSize").ToString();
                    if (ret == "2048")
                        MessageBox.Show("We have the default 2048 value already set for the Buffersize", "Value already present", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        MessageBox.Show("We have a value already set for the Buffersize but its not 2048 that we need to set. Setting it up now", "Value already present", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xbmk.SetValue("BufferSize", "2048", RegistryValueKind.DWord);

                    }


                }
                else //key not present so creating it
                {
                    xbmk.SetValue("BufferSize", "2048", RegistryValueKind.DWord);
                }

                xbmk.Close();
            }
            else  //Registry Key does not exist. Creating it now
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession does not exist. Creating it now");
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession");
                RegistryKey xbmk1 = Registry.LocalMachine;
                xbmk1 = xbmk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
                xbmk1.SetValue("BufferSize", "2048", RegistryValueKind.DWord);
                xbmk1.Close();
            }
            //// end wmi autologger

            // For the Global logger

            RegistryKey xbmk2 = Registry.LocalMachine;
            xbmk2 = xbmk2.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger", true); //opening the key as writeable
            if (xbmk2 != null) // checking if the key exists
            {
                MessageBox.Show("Registry Key HLKM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger exists. Working on the subkey now.");
                xbmk2.SetValue("BufferSize", "2048", RegistryValueKind.DWord);
                xbmk2.Close();
            }//close if

            else
            {

                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger does not exist. Creating it now");
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger");
                RegistryKey xbmk3 = Registry.LocalMachine;
                xbmk3 = xbmk3.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\GlobalLogger", true); //opening the key as writeable
                xbmk3.SetValue("BufferSize", "2048", RegistryValueKind.DWord);
                xbmk3.Close();

            }//close else
            //end global loggger

            //start stack tracing for xbootmgr

            RegistryKey xbmk4 = Registry.LocalMachine;
            xbmk4 = xbmk4.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
            if (xbmk4 != null) // checking if the key exists
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession exists. Working on the subkey now.");
                //checking if the Buffersize already exist                
                if (xbmk4.GetValue("EnableProperty") != null)
                {
                    string ret = xbmk4.GetValue("EnableProperty").ToString();
                    if (ret == "4")
                        MessageBox.Show("We have the default 4 value already set for the EnableProperty", "Value already present", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                    {
                        MessageBox.Show("We have a value already set for the EnableProperty but its not 4 that we need to set. Setting it up now", "Value already present", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xbmk4.SetValue("EnableProperty", "4", RegistryValueKind.DWord);

                    }


                }
                else //key not present so creating it
                {
                    xbmk4.SetValue("EnableProperty", "4", RegistryValueKind.DWord);
                }

                xbmk4.Close();
            }
            else  //Registry Key does not exist. Creating it now
            {
                MessageBox.Show("Registry Key HKLM\\SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSessiondoes not exist. Creating it now");
                RegistryKey xbmk5 = Registry.LocalMachine;
                xbmk5.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession");
                xbmk5 = xbmk5.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\WMI\\Autologger\\XbootMgrSession", true); //opening the key as writeable
                xbmk5.SetValue("EnableProperty", "4", RegistryValueKind.DWord);
                xbmk5.Close();
            }
            //end stack tracing for xbootmgr


        }        //end of the xbmgen function


        // Function to get the trace options selected
        public void traceoptions()
        {
            st = " ";

            // Next show the object title and check state for each item selected.
            foreach (object itemChecked in checkedListBox1.CheckedItems)
            {
                st = st + itemChecked.ToString() + "+";
            }

            st = st.Substring(0, ((st.Length) - 1));

            //if the User selected the stack walk option but didnt select any options, then i am setting it to use
            if ((st.Length) == 0)
                st = "ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate ";

            //close button function
        }



        public Form1()
        {
            InitializeComponent();
            os_name = textBox5.Text;
            lmnt5 = "create counter perf_log -c \"\\Processor(*)\\*\" \"\\Memory\\*\" \"\\PhysicalDisk(*)\\*\" \"\\Process(*)\\*\" \"\\System\\*\" -f bincirc -max 300 -si 00:00:05";
            lmnt6 = "create counter perf_log -c \"\\Processor(*)\\*\" \"\\Memory\\*\" \"\\PhysicalDisk(*)\\*\" \"Process(*)\\*\" \"\\System\\*\" -f bincirc -max 300 -si 00:00:05  -v mmddhhmm";
            lms = "start perf_log";
            lmstp = "stop perf_log";
            ab = 0;
            st = "ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate"; // the default stackwalks
            pt = "";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            time_start = DateTime.Now.ToLongTimeString();

            if (checkBox1.Checked == true)
            {
                os_name = textBox5.Text;
                if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == true)
                    myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", lmnt5);
                else
                    myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", lmnt6);
                //continue starting logman now 
                starttheprocess(1);
                myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", lms);
                starttheprocess(1);
            }

            //   MessageBox.Show("Reproduce the issue now", "Tracing...", MessageBoxButtons.OK, MessageBoxIcon.None);
            button2.Enabled = true;

            if (radioButton1.Checked == true)  // basic mode of logging
            {
                int t = 1;
                switch (comboBox1.SelectedIndex)
                {

                    case 0://base
                        xpe = "xperf.exe";
                        xparg = " -on diageasy -f kernel.etl -BufferSize 1024 -MaxBuffers 1024 -MaxFile 1024 -FileMode Circular";

                        if (checkBox3.Checked == true)
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xparg = xparg + " -stackwalk " + st;
                        }



                        button1.Enabled = false;
                        break;
                    case 1://high disk io
                        xpe = "xperf.exe";
                        xparg = " -on PROC_THREAD+LOADER+CSWITCH+DISK_IO+DISK_IO_INIT+PROFILE+FILENAME+FILE_IO+FILE_IO_INIT+DRIVERS+NETWORK -f kernel.etl -BufferSize 1024 -MaxBuffers 1024 -MaxFile 1024 -FileMode Circular";
                        if (checkBox3.Checked == true)
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xparg = xparg + " -stackwalk " + st;
                        }

                        button1.Enabled = false;
                        break;
                    case 2://high cpu
                        xpe = "xperf.exe";
                        //Xperf -on Latency -f kernel.etl -stackwalk CSwitch+Profile+SyscallEnter
                        xparg = " -on LOADER+PROC_THREAD+DISK_IO+HARD_FAULTS+DPC+INTERRUPT+CSWITCH -f kernel.etl -BufferSize 1024 -MaxBuffers 1024 -MaxFile 1024 -FileMode Circular";
                        if (checkBox3.Checked == true)
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xparg = xparg + " -stackwalk " + st;
                        }

                        button1.Enabled = false; break;

                    case 3: //Paged non-paged on win7 and beyond

                        // checkBox3_CheckedChanged(sender, e );

                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }


                        if (checkBox3.Checked == false) // if stack tracing is not checked then 
                        {
                            if ((os_name.Contains("Windows 7")) || (os_name.Contains("2008 R2")) == true)
                            {
                                xpe = "xperf.exe";
                                ////xparg = " -on base+pool -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024";
                                xparg = " -on base+pool " + pt + " -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            }

                            else
                            {
                                MessageBox.Show("This option is only valid for Windows 7 and Windows Server 2008 R2. Currently only Poolmon ill start if poolmon3vbs files  _LogPool-as-a-service.cmd are present in the same location as this", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                // Need to check if the file is present or not at the location. Else display the message - Please run the file from a directory that contacts the poolmon3 vbs files
                                System.Diagnostics.Process.Start(@"_LogPool-as-a-service.cmd");
                                t = 0;
                            }
                        }

                        if (((checkBox3.Checked == true) && ((os_name.Contains("Windows 7")) || (os_name.Contains("2008 R2")))))
                        {
                            xpe = "xperf.exe";
                            ////xparg = "on base+pool -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024";
                            xparg = " -on base+pool " + pt + " -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024 ";

                            traceoptions(); //only if stack tracing is checked.
                            // if we select stack trace and do not select any option, then run the above command, else append the options
                            if ((st.Length) != 0)
                            {
                                ////xparg = "on base+pool -stackwalk " + st + " -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024";
                                xparg = " -on base+pool " + pt + " -stackwalk " + st + " -f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            }
                        }

                        button1.Enabled = false;
                        break;

                    case 4: //working set 


                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        if (checkBox3.Checked == false)
                        {
                            if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == false)
                            {
                                xpe = "xperf.exe";
                                xparg = " –on PROC_THREAD+LOADER+MEMINFO+CONTMEMGEN -stackwalk ContiguousMemoryGeneration –f kernel.etl  -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            }
                            else
                            {
                                MessageBox.Show("This option is only valid for Windows Vista/7/Windows Server 2008/2008R2 only.", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                t = 0;

                            }
                        }

                        //if (((checkBox3.Checked == true) && ((os_name.Contains("Windows 7")) || (os_name.Contains("2008")) || (os_name.Contains("Vista")))) == true)
                        if (checkBox3.Checked == true)
                        {
                            xpe = "xperf.exe";
                            xparg = " –on PROC_THREAD+LOADER+MEMINFO+CONTMEMGEN -stackwalk ContiguousMemoryGeneration –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            traceoptions(); //only if stack tracing is checked.
                            // if we select stack trace and do not select any option, then run the above command, else append the options
                            if ((st.Length) != 0)
                            {
                                ////xparg = xparg + " -stackwalk " + st;
                                xparg = " –on PROC_THREAD+LOADER+MEMINFO+CONTMEMGEN -stackwalk " + st + "–f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            }

                        }


                        button1.Enabled = false;
                        break;

                    case 5: //heap leak

                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2.", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        if (checkBox3.Checked == false)
                        {
                            if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == false)
                            {

                                xpe = "xperf.exe";
                                MessageBox.Show("Please select the PID of the Process you want to monitor from the next screen", "Process ID Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                               
                                Form2 frm2 = new Form2();
                                frm2.ShowDialog(); // show Form2 
                                hpid = frm2.MaxValue;
                                ht1 = " -on Base -start heapSession -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy -heap -pids ";
                                ht2 = " –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                                xparg = ht1 + hpid + ht2;
                                // xparg = "-on Base -start heapSession -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy -heap -pids <process pid> -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024";

                            }

                            else
                            {
                                MessageBox.Show("This option is only valid for Windows Vista/7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                                t = 0;

                            }
                        }

                        if (checkBox3.Checked == true)
                        {

                            xpe = "xperf.exe";
                            MessageBox.Show("Please select the PID of the Process you want to monitor from the next screen", "Process ID Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Form2 frm2 = new Form2();
                            frm2.ShowDialog(); // show Form2 
                            hpid = frm2.MaxValue;
                            ht1 = " -on Base -start heapSession ";
                            traceoptions();
                            if ((st.Length) != 0)
                                ht1 = ht1 + " -stackwalk " + st;
                            else
                                ht1 = ht1 = " -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy";
                            ht1 = ht1 + " -heap -pids ";
                            ht2 = " –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            xparg = ht1 + hpid + ht2;



                            // xpe = "xperf.exe";
                            //xparg = " –on PROC_THREAD+LOADER+MEMINFO+CONTMEMGEN -stackwalk ContiguousMemoryGeneration –f kernel.etl";
                            //only if stack tracing is checked.
                            // if we select stack trace and do not select any option, then run the above command, else append the options

                        }

                        button1.Enabled = false;
                        break;


                    case 6: // virtual allocations
                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        if (checkBox3.Checked == false)
                        {
                            if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == false)
                            {
                                xpe = "xperf.exe";
                                xparg = " -on BASE+VIRT_ALLOC -stackwalk VirtualAlloc –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";

                            }

                            else
                            {
                                MessageBox.Show("This option is only valid for Windows Vista/7/Windows Server 2008/2008R2 only.", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                                t = 0;

                            }
                        }

                        if (checkBox3.Checked == true)
                        {
                            xpe = "xperf.exe";
                            xparg = " -on BASE+VIRT_ALLOC -stackwalk VirtualAlloc –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            traceoptions(); //only if stack tracing is checked.
                            if ((st.Length) != 0)
                            {
                                //xparg = xparg + " -stackwalk " + st;
                                xparg = " -on BASE+VIRT_ALLOC -stackwalk " + st + " –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024 ";
                            }
                        }
                        button1.Enabled = false;
                        break;



                    case 7: // wait analysis
                        //if (checkBox3.Enabled == false)  // for 2003 -
                        //{
                        //    MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        //    t = 0; //no starting the process.
                        //    button1.Enabled = true;
                        //    button2.Enabled = false;
                        //    xparg = "This option is not supported on your operating system";
                        //    break;
                        //}
                        //else
                        //{

                        //    MessageBox.Show("Wait analysis is in development mode", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        //    t = 0;

                        //}

                        ////    
                        //break;

                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        if (checkBox3.Checked == false)
                        {
                            if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == false)
                            {
                                xpe = "xperf.exe";
                                xparg = " -on Latency+DISPATCHER -stackwalk CSwitch+ReadyThread+ThreadCreate+Profile –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";

                            }

                            else
                            {
                                MessageBox.Show("This option is only valid for Windows Vista/7/Windows Server 2008/2008R2 only.", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                                t = 0;

                            }
                        }

                        if (checkBox3.Checked == true)
                        {
                            xpe = "xperf.exe";
                            xparg = " -on Latency+DISPATCHER -stackwalk CSwitch+ReadyThread+ThreadCreate+Profile –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            traceoptions(); //only if stack tracing is checked.
                            if ((st.Length) != 0)
                            {
                                //xparg = xparg + " -stackwalk " + st;
                                xparg = " -on Latency+DISPATCHER -stackwalk " + st + " –f kernel.etl -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024 -FileMode Circular -MaxFile 1024";
                            }
                        }
                        button1.Enabled = false;
                        break;



                    case 8: //for shutdown delay

                        xbmgen();
                        xparg = " -trace shutdown -noPrepReboot –stackwalk ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate";
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately to start the capture once you click on Yes. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("The computer will shutdown for the capture to begin");
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later");
                            t = 0;
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "shutdown canceled by user";
                        }
                        break;

                    case 9: //For reboot 

                        xbmgen();
                        xparg = " -trace rebootCycle -noPrepReboot –stackwalk ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate";
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately once you click Yes. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("The computer will shutdown for the capture to begin");
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later");
                            t = 0;
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "Restart canceled by user";
                        }

                        break;

                    case 10: //for startup delay

                        xbmgen();
                        xparg = " -trace boot -traceFlags DiagEasy+DISPATCHER+DPC -postBootDelay 240 –stackwalk ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate";
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately once you click Yes. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("The computer will shutdown for the capture to begin", "Shutting down", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later", "Xperf123");
                            t = 0;
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "Restart canceled by user";
                        }
                        break;
                    case 11: //hibernation

                        xbmgen();
                        xparg = " –trace hibernate –stackwalk ProcessCreate+CSwitch+ReadyThread+Mark+SyscallEnter+ThreadCreate";
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will hibernate immediately once you click on Yes. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("The computer will hibernate for the capture to begin", "Shutting down", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later", "Xperf123");
                            t = 0;
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "Hibernate canceled by user";
                        }
                        break;

                }//switch end
                //textBox6.Text = xparg;
                myProcess.StartInfo = new ProcessStartInfo(xpe, xparg);
                starttheprocess(t);


            }
            // for advanced mode ----

            if (radioButton2.Checked == true)
            {
                t = 1;
                xppara1 = " -BufferSize " + textBox1.Text;
                xppara2 = " -MaxBuffers " + textBox2.Text;
                xppara3 = " -MaxFile " + textBox3.Text;
                xpfile = " -f kernel.etl";
                if (checkBox2.Checked == true)
                    xpmode = " -FileMode Circular";
                else xpmode = "";

                targ = xpfile + xppara1 + xppara2 + xppara3 + xpmode;

                switch (comboBox1.SelectedIndex)
                {

                    case 0:
                        // base

                        xptype = " -on diageasy";
                        if (checkBox3.Checked == true)
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xptype = xptype + " -stackwalk " + st;
                        }

                        break;

                    case 1:
                        //Disk
                        xptype = " -on PROC_THREAD+LOADER+CSWITCH+DISK_IO+DISK_IO_INIT+PROFILE+FILENAME+FILE_IO+FILE_IO_INIT+DRIVERS+NETWORK";
                        if (checkBox3.Checked == true) // if stack tracing option selected
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xptype = xptype + " -stackwalk " + st;
                        }
                        break;

                    case 2:
                        // high cpu
                        xptype = " -on LOADER+PROC_THREAD+DISK_IO+HARD_FAULTS+DPC+INTERRUPT+CSWITCH";
                        if (checkBox3.Checked == true)  // if stack tracing option selected
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xptype = xptype + " -stackwalk " + st;
                        }
                        break;

                    case 3: //Paged non-paged to add it more


                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        if (checkBox3.Checked == false) // if stack tracing is not checked then 
                        {
                            if ((os_name.Contains("Windows 7")) || (os_name.Contains("2008 R2")) == true)
                            {
                                xpe = "xperf.exe";
                                ////xptype = " -on base+pool -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession";

                                xptype = " -on base+pool " + pt + " -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession";

                            }

                            else
                            {
                                MessageBox.Show("This option is only valid for Windows 7 and Windows Server 2008 R2. Currently only Poolmon ill start if poolmon3vbs files  _LogPool-as-a-service.cmd are present in the same location as this", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                // Need to check if the file is present or not at the location. Else display the message - Please run the file from a directory that contacts the poolmon3 vbs files
                                System.Diagnostics.Process.Start(@"_LogPool-as-a-service.cmd");
                                t = 0;

                            }
                        }


                        if ((checkBox3.Checked == true) && (((os_name.Contains("Windows 7")) || (os_name.Contains("2008 R2")) == true)))
                        {
                            xptype = " -on base+pool -stackwalk PoolAlloc+PoolAllocSession+PoolFree+PoolFreeSession";
                            traceoptions();
                            if ((st.Length) != 0)
                            {//// xptype = xptype + " -stackwalk " + st;
                                xptype = " -on base+pool -stackwalk " + st;
                            }
                            else
                            {
                            }
                        }


                        break;

                    case 4: //working set 
                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }
                        xptype = " –on PROC_THREAD+LOADER+MEMINFO+CONTMEMGEN";
                        if (checkBox3.Checked == true)  // if stack tracing option selected
                        {
                            traceoptions(); //only if stack tracing is checked.
                            if ((st.Length) != 0)
                                xptype = xptype + " -stackwalk " + st;
                        }
                        break;


                    case 5: //heap leak


                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        MessageBox.Show("Please select the PID of the Process you want to monitor from the next screen", "Process ID Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Form2 frm2 = new Form2();
                        frm2.ShowDialog(); // show Form2 
                        hpid = frm2.MaxValue;
                      //   ht1 = " -on Base -start heapSession -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy -heap -pids ";
                        ht1 = " -on Base -start heapSession ";
                        traceoptions();

                        if ((st.Length) != 0)
                            ht1 = ht1 + " -stackwalk " + st;
                        else
                            ht1 = ht1 = " -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy";
                        ht1 = ht1 + " -heap -pids ";
                        xptype = ht1 + hpid;

                        // xparg = "-on Base -start heapSession -stackwalk HeapCreate+HeapAlloc+HeapRealloc+HeapFree+HeapDestroy -heap -pids <process pid> -BufferSize 1024 -MinBuffers 1024 -MaxBuffers 1024";
                        break;

                    case 6: // virtual allocations

                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        xptype = " -on BASE+VIRT_ALLOC";
                        if (checkBox3.Checked == true)  // if stack tracing option selected
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xptype = xptype + " -stackwalk " + st;
                        }
                        break;





                    case 7: // wait analysis

                        if (checkBox3.Enabled == false)  // for 2003 -
                        {
                            MessageBox.Show("This option is only valid for Windows Vista/Windows 7/Windows Server 2008/2008R2", "WAIT", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            t = 0; //no starting the process.
                            button1.Enabled = true;
                            button2.Enabled = false;
                            xparg = "This option is not supported on your operating system";
                            break;
                        }

                        xptype = " -on Latency+DISPATCHER";
                        if (checkBox3.Checked == true)  // if stack tracing option selected
                        {
                            traceoptions(); //only if stack tracing is checked.
                            xptype = xptype + " -stackwalk " + st;
                        }
                        break;


                    case 8: //for shutdown delay
                        m = 2;
                        xbmadv();

                        if (checkBox3.Checked == true)
                            traceoptions(); //only if stack tracing is checked.
                        xparg = " -trace shutdown -noPrepReboot -stackwalk " + st;
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately once you click on Yes. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("Please Click on Start Capture to shutdown the system for the capture to begin");
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later");
                            t = 0;
                        }
                        break;

                    case 9: //For reboot 
                        m = 2;
                        xbmadv();
                        if (checkBox3.Checked == true)
                            traceoptions(); //only if stack tracing is checked.
                        xparg = " -trace rebootCycle -noPrepReboot -stackwalk " + st;
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately once you click on Start Capture. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("Please Click on Start Capture to shutdown the system for the capture to begin", "Shutting down", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later", "Xperf123");
                            t = 0;
                        }

                        break;

                    case 10: //for startup delay
                        m = 2;
                        xbmadv();

                        if (checkBox3.Checked == true)
                            traceoptions(); //only if stack tracing is checked.

                        xparg = " -trace boot -traceFlags DiagEasy+DISPATCHER+DPC -postBootDelay 240 –stackwalk " + st;
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will reboot immediately once you click on Start Capture. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("Please Click on Start Capture to shutdown the system for the capture to begin", "Shutting down", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later", "Xperf123");
                            t = 0;
                        }
                        break;

                    case 11: //hibernation
                        m = 2;
                        xbmadv();

                        if (checkBox3.Checked == true)
                            traceoptions(); //only if stack tracing is checked.

                        xparg = " –trace hibernate -stackwalk " + st;
                        xpe = "xbootmgr.exe";
                        msgr = MessageBox.Show("The machine will hibernate immediately once you click on Start Capture. The files will be placed in the same directory from where you launched Xperf123. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        //Do we really want to reboot just now?  
                        if (msgr == DialogResult.Yes)
                            MessageBox.Show("Please Click on Start Capture to hibernate the system for the capture to begin", "Shutting down", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                        {
                            MessageBox.Show("Please run Auto_XPerf again later", "Xperf123");
                            t = 0;
                        }
                        break;



                } // switch ends

                if (m == 1)
                {
                    xparg = xptype + targ;
                    textBox6.Text = xparg;
                    myProcess.StartInfo = new ProcessStartInfo(@"xperf.exe", xparg);
                    starttheprocess(1);
                }
                if (m == 2)
                {
                    textBox6.Text = xparg;
                    myProcess.StartInfo = new ProcessStartInfo(xpe, xparg);
                    starttheprocess(t);
                }

                if (t != 0)
                    button1.Enabled = false;
                else
                {
                    button1.Enabled = true;
                    button2.Enabled = false;
                }

            } //close advanced if


        } //end button1 click function


        private void button2_Click(object sender, EventArgs e)
        {

            //locating the location to save the file
            if (checkBox4.Checked == true)
            {
                //creating the text file
                fp = Directory.GetCurrentDirectory();
                FolderBrowserDialog fb = new FolderBrowserDialog();
                fb.Description = "Select destination folder to save the logs";
                fb.RootFolder = Environment.SpecialFolder.MyComputer;
                fb.SelectedPath.Equals(fp);

                if (fb.ShowDialog() == DialogResult.OK)
                {
                    fpath = fb.SelectedPath;
                }

            }
            else
                fpath = Directory.GetCurrentDirectory();
            //Now that i know where to save the file, lets continue with the rest of the action

            string tname;
            cd = DateTime.Now.ToShortDateString();
            cd = cd.Replace(" ", "_");
            cd = cd.Replace("/", "_");
            cd = cd.Replace("\\", "_");
            tn = textBox7.Text + "-" + cd + "-" + textBox9.Text.Remove(7).Replace(":", "_");
            tname = tn + ".txt";
            fname = fpath + "\\" + tname;
            xppath = " -d " + fpath + "\\" + tn + "-" + "Trace.etl";
            button2.Enabled = false;
            fp = Directory.GetCurrentDirectory();
            xpe = fp + "\\" + "xperf.exe";
            if (checkBox4.Checked == true)
            {
                textBox6.Text = xppath;
                myProcess.StartInfo = new ProcessStartInfo(xpe, xppath);
                starttheprocess(1);
            }

            else
            {
                textBox6.Text = xppath;
                myProcess.StartInfo = new ProcessStartInfo(@"xperf.exe", xppath);
                starttheprocess(1);
            }

            button1.Enabled = true;
            //stop the perfmon if it was started
            if (checkBox1.Checked == true)
            {
                myProcess.StartInfo = new ProcessStartInfo(@"logman.exe", lmstp);
                starttheprocess(1);
            }

            // System.Diagnostics.Process.Start(@"copy c:\\perflogs\\perf_log* gPool-as-a-service.cmd");

            if (fname != null)
            {
                StreamWriter sw = new FileInfo(fname).CreateText();
                sw.WriteLine("=============================================");
                sw.Write("Computer Name: "); sw.WriteLine(textBox7.Text);
                sw.Write("User Name: "); sw.WriteLine(textBox8.Text);
                sw.WriteLine("Xperf123 Version: 1.0");
                sw.Write("Operating System: "); sw.WriteLine(textBox5.Text);
                sw.Write("Time Started: "); sw.WriteLine(time_start);
                sw.Write("Command line parameter passed: "); sw.WriteLine(xparg);
                sw.Write("Stack Walk: ");
                if (checkBox3.Checked == true)
                    sw.WriteLine("Yes");
                else
                    sw.WriteLine("No");
                sw.Write("Perfmon Enabled: ");
                if (checkBox1.Checked == true)
                    sw.WriteLine("Yes");
                else
                    sw.WriteLine("No");
                time_stop = DateTime.Now.ToLongTimeString();
                sw.Write("Time logging finished: "); sw.WriteLine(time_stop);
                sw.WriteLine("");
                sw.Flush();
                sw.Close();
            }

            //open the directory where the files are present
            System.Diagnostics.Process.Start(@fpath);


        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if ((radioButton2.Checked == true) && (ab < 8)) //advanced selected and xperf options selected
            {
                groupBox1.Visible = true;
                groupBox4.Visible = false;
                groupBox3.Visible = true;

            }

            if ((radioButton2.Checked == true) && (ab >= 8)) //advanced selected and xbootmgr option selected 
            {
                groupBox4.Visible = true;
                groupBox1.Visible = false;
                groupBox3.Visible = false;
            }
            button1.Enabled = true;
            button2.Enabled = false;


            //int v;
            //v = comboBox1.SelectedIndex;
            //if (v < 6)
            //{
            //    groupBox3.Visible = true;
            //}
            //else
            //{
            //    groupBox4.Visible = true; 
            //}


            //button1.Enabled = true;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            comboBox1.SelectedIndex = 0;
            textBox4.Text = System.Environment.OSVersion.VersionString.ToString();
            textBox7.Text = System.Environment.MachineName.ToString();
            textBox8.Text = System.Environment.UserName.ToString();
            textBox10.Text = System.Environment.Version.ToString();


            RegistryKey rk = Registry.LocalMachine;
            rk = rk.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", true);
            textBox5.Text = rk.GetValue("ProductName").ToString();
            rk.Close();
            os_name = textBox5.Text;
            if ((os_name.Contains("Windows Server 2003")) || (os_name.Contains("Windows XP")) || (os_name.Contains("Windows 2000")) == true)
                checkBox3.Enabled = false;
            radioButton1.Checked = true;
            button1.Enabled = true;


        }// close form load

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //for the basic checkbox


            if (ab < 8) // for xperf basic 
            {
                groupBox3.Visible = true;
                groupBox1.Visible = false;
                groupBox4.Visible = false;

            }
            else  //for xbootmgr basic
            {
                groupBox3.Visible = false;
                groupBox1.Visible = false;
                groupBox4.Visible = false;
            }

            button1.Enabled = true;
            button2.Enabled = false;



        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //buffersize = Convert.ToInt32(textBox1.Text);

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //            maxbuffer = Convert.ToInt32(textBox2.Text);

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            //          maxfile = Convert.ToInt32(textBox3.Text);

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox9.Text = DateTime.Now.ToLongTimeString();
            // DateTime d2 = DateTime.Now;
         }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (t == 1)
                System.Diagnostics.Process.Start(@"xperf.exe", "stopall /silent");

        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked == true)
            {
                checkedListBox1.Visible = true;
                // checkedListBox1.Enabled = true;

                if (IntPtr.Size == 8)
                {
                    archi = "64 bit machine";
                    //setting the reg key now - 
                    RegistryKey rk = Registry.LocalMachine;
                    rk = rk.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management", true);
                    //String[] Prnpr = rk.GetValueNames();
                    //Searching for Disable Paging executive
                    //  foreach (String P in Prnpr)

                    if (rk.GetValue("DisablePagingExecutive") != null)
                    {
                        string ret = rk.GetValue("DisablePagingExecutive").ToString();
                        if (ret == "0")
                        {
                            rk.SetValue("DisablePagingExecutive", 1);
                            //rk.Close();
                            DialogResult msga = MessageBox.Show("DisablePagingExecutive value set to 1. The machine requires to be rebooted for the setting to be enabled.This setting is only for troubleshooting purposes. Once troubleshooting is done, please set the value back to 0. Do you want to reboot now", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                            if (msga == DialogResult.Yes)
                            {
                                textBox6.Text = " Yes, continue ";
                                DialogResult msgr = MessageBox.Show("The machine will reboot now in 60 seconds. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                //Do we really want to reboot just now?  
                                if (msgr == DialogResult.Yes)
                                    System.Diagnostics.Process.Start("shutdown.exe", "-r -t 60");
                                else
                                    MessageBox.Show("Please reboot the machine after making all the changes", "Xperf123");
                            }
                            else if (msga == DialogResult.No)
                                textBox6.Text = " No, stop";
                        }

                        else if (ret == "1")
                            MessageBox.Show("DisablePagingExective Value already set to 1.The location of the registry HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\DisablePagingExecutive (dword) 0 (hex) is the default setting. This setting 1 is for troubleshooting only. Once troubleshooting is finished, you need to change it back to the default setting or your system can experience performance problems.", "Information", MessageBoxButtons.OK);

                    }
                    else
                    {
                        rk.SetValue("DisablePagingExecutive", 1);
                        //rk.Close();
                        DialogResult msgb = MessageBox.Show("For stackwalking on 64-bit the DisablePagingExecutive (dword) was changed from 0 (hex) to 1 (hex).Note: The location of the registry HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management\\DisablePagingExecutive (dword) 0 (hex) is the default setting.This setting is for troubleshooting only.  Once troubleshooting is finished, you need to change it back to the default setting or your system can experience performance problems. Windows need to be restarted, do you want to restart now?", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (msgb == DialogResult.Yes)
                        {
                            textBox6.Text = " Yes, continue ";
                            DialogResult msgr = MessageBox.Show("The machine will reboot now in 60 seconds. Do you want to continue", "Reboot required", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                            //Do we really want to reboot just now?  
                            if (msgr == DialogResult.Yes)
                                System.Diagnostics.Process.Start("shutdown.exe", "-r -t 60");
                            else
                                MessageBox.Show("Please reboot the machine after making all the changes", "Xperf123");
                        }

                    } rk.Close();

                }

                else if (IntPtr.Size == 4)
                {
                    archi = "32 bit machine";
                }


                MessageBox.Show("The size of the parameter for Stackwalk is limited to 512 only. Please select only the required stack traces", "Wait", MessageBoxButtons.OK, MessageBoxIcon.Warning);


            } // end if

            else
            {
                // checkedListBox1.Enabled = false;
                checkedListBox1.Visible = false;
            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
     

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //foreach(  in 6,7,8,9)
            ab = comboBox1.SelectedIndex;

            if (checkBox3.Enabled == false)// for 2003 -
            {  // do nothing 
            }
            else
            {
                //for the pool usage
                if (ab == 3)
                    checkBox5.Visible = true;
                else
                    checkBox5.Visible = false;

                // for stack walks
                if (ab == 5)
                    checkBox3.Checked = true;
                else
                    checkBox3.Checked = false;

                if (ab < 8) // for xperf related actions
                {
                    //Assuming that the basic option is selected then
                    groupBox3.Visible = true;
                    groupBox1.Visible = false;
                    groupBox4.Visible = false;

                    if (radioButton2.Checked == true) // if the advanced is selected then 
                    {

                        groupBox3.Visible = true;
                        groupBox1.Visible = true;
                        groupBox4.Visible = false;
                    }
                    else  // 
                    {

                    }

                }
                if (ab > 7) // for xbootmgr related options
                {

                    //assuming that the basic is selected
                    groupBox3.Visible = false;
                    groupBox1.Visible = false;
                    groupBox4.Visible = false;
                    if (radioButton2.Checked == true) //if advanced is selected and xbootmgr optionsToolStripMenuItem are on
                    {
                        groupBox3.Visible = false;
                        groupBox1.Visible = false;
                        groupBox4.Visible = true;

                    }
                    else  // if the basic option is selected then
                    {


                    }
                }

            }// close the else loop

        } // close of select index



        private void newToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void exitToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Xperf123.AboutBox1  fAbtFrm = new Xperf123.AboutBox1();
            fAbtFrm.ShowDialog(); // show Form2 
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Please contact Digvijan for obtaining the latest bits", "Update...", MessageBoxButtons.OK, MessageBoxIcon.None);

        }


        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox5.Checked == true)
                pt = Microsoft.VisualBasic.Interaction.InputBox("Please enter the pooltags you want to monitor in the format: -PoolTag <P1>+<P2>+<P3>+...+<Pn>", "Enter Pooltags", "-PoolTag ", 350, 350);
            else
                pt = "";
        }

        private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Currently Inactive");
        }

        private void customizeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void aboutToolStripMenuItem3_Click_1(object sender, EventArgs e)
        {
            Xperf123.AboutBox1 fAbtFrm = new Xperf123.AboutBox1();
            fAbtFrm.ShowDialog(); // show Form2 
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBox5_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox5.Checked == true)
                pt = Microsoft.VisualBasic.Interaction.InputBox("Please enter the pooltags you want to monitor in the format: -PoolTag <P1>+<P2>+<P3>+...+<Pn>", "Enter Pooltags", "-PoolTag ", 350, 350);
            else
                pt = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            textBox13.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int a = tabControl1.SelectedIndex;
            if (!tabControl1.SelectedIndex.Equals(0))
                a--;
            tabControl1.SelectedIndex = a;

            if (tabControl1.SelectedIndex.Equals(0))
            {
                button5.Text = "Start";
         
            }
            else
                button5.Text = "Previous";


            if (tabControl1.SelectedIndex.Equals(5))
                button6.Text = "Finish";
            else
                button6.Text = "Next";
        }

        private void button6_Click(object sender, EventArgs e)
        {

            
            int a = tabControl1.SelectedIndex;
            if (!tabControl1.SelectedIndex.Equals(5))
            a++;
            tabControl1.SelectedIndex = a;
            if (tabControl1.SelectedIndex.Equals(5))
               button6.Text = "Finish";
            else 
                button6.Text = "Next";

            if (tabControl1.SelectedIndex.Equals(0))
                button5.Text = "Start";
            else
                button5.Text = "Previous";
        }


    }

}
/*
HeapCreate
HeapAlloc
HeapRealloc
HeapFree
HeapDestroy
ProcessCreate
CSwitch
ReadyThread
Mark
SyscallEnter
ThreadCreate
*/